#ifndef VERTEXITEM_H
#define VERTEXITEM_H


class LabelScene;

#include <QGraphicsPathItem >
#include <QPointF>
class PolygonItem;
class VertexItem : public QObject, public QGraphicsPathItem
{
public:
    enum { Type = UserType + 3 };
    int type() const override { return Type; }
    enum State { Unplaced, Placed, Hovered, Moved  };
    enum Shape { Circle, Rectangle };
    VertexItem(QPointF pos, State state, LabelScene* scene, QGraphicsItem *parent = nullptr);
    VertexItem(QPointF pos, State state, QGraphicsItem *parent = nullptr);
    VertexItem(QPointF pos, QGraphicsItem *parent = nullptr);
    VertexItem(QPointF pos, State state, LabelScene* scene, qreal size, QGraphicsItem *parent = nullptr);


    QPainterPath shape() const override{
        return path;
    }
    QRectF boundingRect() const override{
        return path.boundingRect();
    }
    void setScene(LabelScene* scene) {myScene = scene;}
    LabelScene* scene() {return myScene;}
    void setState(State state);
    State state() {return myState; }
    void setIndex(qreal index) {myIndex = index;}
    qreal index() {return myIndex;}
    void setPolygonItem(PolygonItem* polygonItem){myPolygonItem = polygonItem;}
    PolygonItem* polygonItem() {return myPolygonItem;}

    void updateStyle();
    void updateShape(Shape shape);
    void updateUnplacedStyle(QColor unplacedPenColor, QColor unplacedBrushColor, Shape unplacedShape);
    void updatePlaceStyle(QColor placedPenColor, QColor placedBrushColor, Shape placedShape);
    void updateHoveredStyle(QColor hoveredPenColor, QColor hoveredBrushColor, Shape hoveredShape);



protected:
    void hoverEnterEvent(QGraphicsSceneHoverEvent *event) override;
    void hoverLeaveEvent(QGraphicsSceneHoverEvent *event) override;
    void mousePressEvent(QGraphicsSceneMouseEvent* event) override;
    QVariant itemChange(GraphicsItemChange change, const QVariant &value) override;

private:
    void updateShape(bool isSquare);
    void setSize(qreal size);
    LabelScene* myScene;
    PolygonItem* myPolygonItem;
    qreal myIndex = -1;
    qreal mySize;
    State myState = Unplaced;
    qreal penWidth = 2;
    QPainterPath path;
    QColor unplacedPenColor = QColor(255, 215, 0);
    QColor placedPenColor = QColor(255, 215, 0);
    QColor hoveredPenColor = Qt::white;
    QColor movedPenColor = Qt::white;
    QColor unplacedBrushColor = QColor(0, 0, 0, 0);
    QColor placedBrushColor = QColor(255, 215, 0);
    QColor hoveredBrushColor = Qt::blue;
    Shape unplacedShape = Circle;
    Shape placedShape = Circle;
    Shape hoveredShape = Rectangle;

};

#endif // VERTEXITEM_H
